const path = require('path');
const webpack = require('webpack');

const serverConfig = {
  mode: 'production',
  target: 'node',
  entry: [path.resolve(__dirname, '../scripts/start')],
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: 'server.js',
  },
  externals: [
    function(ctx, request, callback) {
      if (/^koa$/.test(request)) {
        return callback(null, 'commonjs ' + request);
      }
      return callback();
    },
  ],
  module: {
    rules: [
      {
        test: /\.ts$/,
        enforce: 'pre',
        use: [
          {
            loader: 'tslint-loader',
            options: {},
          },
        ],
      },
      {
        test: /\.ts$/,
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
              configFile: path.resolve(__dirname, './tsconfig.json'),
            },
          },
        ],
        exclude: /node_modules/,
      },
      {
        test: /\.jsx?$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js', '.json'],
  },
  plugins: [new webpack.NoEmitOnErrorsPlugin()],
};

module.exports = serverConfig;
